﻿using FreeRedis;
using Masuit.Tools;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Common.Redis
{
    public static class AddRedisExtension
    {
        public static void AddRedis(this IServiceCollection services,IConfiguration config)
        {
            var connectionString = config["Redis:ConnectionString"];
            if (connectionString.IsNullOrEmpty())
            {
                throw new Exception("Redis连接字符串为空");
            }

            services.AddSingleton(sp=> new RedisClient(connectionString));
        }

        public static void AddRedis(this IServiceCollection services,string connectionString)
        {
            if (connectionString.IsNullOrEmpty())
            {
                throw new Exception("Redis连接字符串为空");
            }

            services.AddScoped(sp=> new RedisClient(connectionString));
        }

    }
}
